package com.g.jnitext;

import java.util.Timer;
import java.util.TimerTask;


public class Open {
    private static final int LEVEL_HIGH = 1;
    private static final int LEVEL_LOW = 0;
    private Boolean isNew7s =false;


    public Open(Boolean isNew)
    {
        isNew7s = isNew;
    }

    public void openPrint() {
        if (isNew7s)
        {
            SetGPIO(3, LEVEL_LOW,911);
            SetGPIO(112, LEVEL_HIGH,911);
        }
        else
        {
            SetGPIO(88, LEVEL_HIGH,0);
        }
        //  SetGPIO(88, LEVEL_HIGH);
    }

    public void closePrint() {
        if (isNew7s)
        {
            SetGPIO(112, LEVEL_LOW,911);
        }
        else
        {
            SetGPIO(88, LEVEL_LOW,0);
        }
        // SetGPIO(88, LEVEL_LOW);
    }

    //开启扫描头
    public void openScan() {
        if (isNew7s)
        {
            SetGPIO(3, LEVEL_HIGH,911);
            SetGPIO(0, LEVEL_HIGH,911);

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                               public void run() {
                                   SetGPIO(1, LEVEL_LOW,911);
                               }
                           },
                    150);
        }
        else
        {
            SetGPIO(9, LEVEL_HIGH,0);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                               public void run() {
                                   SetGPIO(8, LEVEL_LOW,0);
                               }
                           },
                    150);
        }
//
//        SetGPIO(9, LEVEL_HIGH);
//        Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//                           public void run() {
//                               SetGPIO(8, LEVEL_LOW);
//                           }
//                       },
//                150);
    }

    //关闭扫描头
    public void closeScan() {
        if (isNew7s)
        {
            SetGPIO(0, LEVEL_LOW,911);
        }
        else
        {
            SetGPIO(9, LEVEL_LOW,0);
        }
        //  SetGPIO(9, LEVEL_LOW);
    }

    //开启扫描
    public void startScan() {
        if (isNew7s)
        {
            SetGPIO(3, LEVEL_HIGH,911);
            SetGPIO(0, LEVEL_HIGH,911);
            SetGPIO(1, LEVEL_HIGH,911);
        }
        else
        {
            SetGPIO(8, LEVEL_HIGH,0);
        }
//        SetGPIO(8, LEVEL_HIGH);
    }

    //停止扫描
    public void stopScan() {
        if (isNew7s)
        {
            SetGPIO(1, LEVEL_LOW,911);
        }
        else
        {
            SetGPIO(8, LEVEL_LOW,0);
        }
        //  SetGPIO(8, LEVEL_LOW);
    }


    private int SetGPIO(int gpio, int level,int offset)
    {
        int ioffset=offset;
//        if (offset==911)
//            ioffset=0;
//        else
//            ioffset=-911;
        return  nativeSetGPIO(gpio+ioffset,level);
    }

    public native int nativeSetGPIO(int gpio, int level);

    //todo cpp文件 SetGPIO.cpp中的 #define GPIO_OFFSET 911;  此值之前是0，现在是911（两个最好都试试）
    static {
        System.loadLibrary("SetGPIO");
//        boolean isNew7S = Build.MODEL.equals("msm8909_512") && Build.VERSION.RELEASE.equals("5.1.1") && Build.MANUFACTURER.equals("unknown");
//        if (isNew7S) {
//            System.loadLibrary("setGPIO");
//        }
    }
//    static {
//        System.loadLibrary("setGPIO");
//    }
}
